package com.xiaoyu.lanling.feature.coin.activity

import android.os.Bundle
import com.xiaoyu.base.app.AppContext
import com.xiaoyu.base.event.AppEventBus
import com.xiaoyu.base.event.SimpleEventHandler
import com.xiaoyu.base.utils.extensions.getViewTag
import com.xiaoyu.base.utils.extensions.setOnClickDebounceListener
import com.xiaoyu.base.utils.extensions.setViewTag
import com.xiaoyu.lanling.R
import com.xiaoyu.lanling.activity.base.AppCompatToolbarActivity
import com.xiaoyu.lanling.common.Constant
import com.xiaoyu.lanling.event.pay.PreparePayEvent
import com.xiaoyu.lanling.feature.coin.model.CoinProductItem
import com.xiaoyu.lanling.feature.pay.data.PayData
import com.xiaoyu.pay.ALI_PAY
import com.xiaoyu.pay.PayUtil
import com.xiaoyu.pay.WE_CHAT_PAY
import kotlinx.android.synthetic.main.activity_coin_charge.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class CoinChargeActivity : AppCompatToolbarActivity() {

    private val requestTag = Any()
    private val payUtil = PayUtil()

    override fun onCreateSafelyAfterAppFinishInit(savedInstanceState: Bundle?) {
        super.onCreateSafelyAfterAppFinishInit(savedInstanceState)
        setLightStatusBar()
        setContentView(R.layout.activity_coin_charge)
        initToolbar()

        initView()
        initBind()
        initEvent()
        initData()
    }

    private fun initBind() {
        ali_pay_layout.setOnClickDebounceListener { v ->
            val coinProductItem = v.getViewTag<CoinProductItem>()
            coinProductItem?.let {
                PayData.preparePay(requestTag, it.productId, ALI_PAY, Constant.From.COIN_CHARGE)
            }
        }
        wechat_pay_layout.setOnClickDebounceListener { v ->
            val coinProductItem = v.getViewTag<CoinProductItem>()
            coinProductItem?.let {
                PayData.preparePay(requestTag, it.productId, WE_CHAT_PAY, Constant.From.COIN_CHARGE)
            }
        }
    }

    private fun initView() {
        setTitle(R.string.coin_charge_title)
    }

    private fun initData() {
        val coinProductItem = intent.getSerializableExtra(Constant.IntentKey.KEY_COIN_PRODUCT_ITEM) as? CoinProductItem
        coin_title.text = coinProductItem?.subject
        coin_price.text = AppContext.getString(R.string.coin_product_price_prefix, coinProductItem?.price)
        ali_pay_layout.setViewTag(coinProductItem)
        wechat_pay_layout.setViewTag(coinProductItem)
    }

    private fun initEvent() {
        AppEventBus.bindContainerAndHandler(this, object : SimpleEventHandler() {
            @Subscribe(threadMode = ThreadMode.MAIN)
            fun onEvent(event: PreparePayEvent) {
                if (event.isNotFromThisRequestTag(requestTag)) {
                    return
                }

                when (event.payWay) {
                    ALI_PAY -> payUtil.toAliPay(event.params)
                    WE_CHAT_PAY -> payUtil.toWeChatPay(event.params)
                }
            }
        })
    }
}